/**
 * 
 */
package smart_campus_api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.zhiteng.entity.School;
import com.zhiteng.entity.Teacher;
import com.zhiteng.entity.Term;
import com.zhiteng.entity.User;
import com.zhiteng.entity.Year;
import com.zhiteng.service.SchoolService;
import com.zhiteng.service.TeacherService;
import com.zhiteng.utils.common.HttpClientUtil;
import com.zhiteng.utils.tencent.campus.CommonResult;
import com.zhiteng.utils.tencent.campus.CommonResult2;
import com.zhiteng.utils.tencent.campus.Data;
import com.zhiteng.utils.tencent.campus.SignUtil;

/**  
* <p>Title: ImportAllSchool.java</p>  
* <p>Description: </p>  
* @author dragon  
* @date 2018年11月7日  
* @version 1.0  
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring/*.xml")
public class ImportData {
	@Autowired
	private SchoolService schoolService;
	@Autowired
	private TeacherService teacherService;
	
	@Value("${devCode}")
	private String devCode;
	@Value("${devType}")
	private String devType;
	@Value("${objType}")
	private String objType;
	@Value("${officeid}")
	private String officeid;
	@Value("${keyForData}")
	private String keyForData;
	
	@Value("${getAllSchoolURL}")
	private String getAllSchoolURL;
	@Value("${getSchoolInfoURL}")
	private String getSchoolInfoURL;
	
	//	将所有学校信息导入到数据库（getObjectInfo和buGetschoolList两个API数据合并）
	@Test
	public void importAllSchool() {
		long timestamp =  System.currentTimeMillis();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("timestamp", timestamp);
		map.put("devCode", devCode);
		map.put("devType", devType);
		map.put("objType", objType);
		map.put("officeid", officeid);
		map.put("size", "100");
		String url = getAllSchoolURL
					+ "timestamp=" + timestamp 
					+ "&sign=" + SignUtil.getSign(map,keyForData) 
					+ "&devCode=" + devCode
					+ "&devType=" + devType
					+ "&objType=" + objType
					+ "&officeid=" + officeid
					+ "&size=100";
		String jsonStr = HttpClientUtil.get(url);
		
		//	把json数据封装成CommonResult
		//System.out.println(jsonStr);
		CommonResult<School> result = JSON.parseObject(jsonStr, new TypeReference<CommonResult<School>>(){});
		//	获取所有学校的集合
		List<School> schoolList = result.getData().getDataList();
		
		for(int i=0;i<schoolList.size();i++) {
			School school = getSchoolInfoById(schoolList.get(i).getObjectid());
			if(school!=null) {
				schoolList.get(i).setEdu_type(school.getEdu_type());;
				schoolList.get(i).setLogo(school.getLogo());
			}
			System.out.println(schoolList.get(i));
		}
		
		//System.out.println(schoolList);
		
		int count = schoolService.saveBatch(schoolList);
		System.out.println("成功插入"+count+"条数据！");
		long endTime = System.currentTimeMillis();
		System.out.println("共耗时"+(endTime-timestamp)+"毫秒！"); //4~5s
	}
	
	public School getSchoolInfoById(String objectid) {
		long timestamp =  System.currentTimeMillis();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("timestamp", timestamp);
		map.put("devCode", devCode);
		map.put("devType", devType);
		map.put("objType", objType);
		map.put("objectid", objectid);
		map.put("size", "100");
		String url = getSchoolInfoURL
					+ "timestamp=" + timestamp 
					+ "&sign=" + SignUtil.getSign(map,keyForData) 
					+ "&devCode=" + devCode
					+ "&devType=" + devType
					+ "&objType=" + objType
					+ "&objectid=" + objectid
					+ "&size=100";
		String jsonStr = HttpClientUtil.get(url);
		
		//	把json数据封装成CommonResult2
		//System.out.println(jsonStr);
		CommonResult2<School> result = JSON.parseObject(jsonStr, new TypeReference<CommonResult2<School>>(){});
		School school = null;
		if(result!=null) {
			school = result.getData();
		} 
		//System.out.println(school.getLogo());
		return school;
	}
	
	
	
	
	
	
	

	
	//	将所有学校的所有在校老师(不包括退休老师)导入到数据库
	@Test
	public void importAllTeacher() {
		long startTime = System.currentTimeMillis();
		//	获取所有学校
		List<School> schoolList = schoolService.getAllSchool();
		List<Teacher> teacherList = new ArrayList<>();
		for (School school : schoolList) {
			//	获取该学校的所有用户列表（老师，学生，退休老师，校友）
			Data<User> data = getUserList(school.getObjectid()).getData();
			if(data!=null) { 	//	可能是null
				System.out.println(school.getObjectid());
				List<User> userList = data.getDataList();
				for (User user : userList) {
					if(user.getUsertype()==2) { 	//	老师（不包括退休老师）
						
						//	若是老师则转为老师对象并添加到list集合中
						Teacher teacher = new Teacher();
						teacher.setCellphone(user.getCellphone());
						teacher.setDepartid(user.getDepartid());
						teacher.setDuty_type(user.getDuty_type());
						teacher.setGender(user.getGender());
						teacher.setHead(user.getHead());
						teacher.setIs_subscribe(user.getIs_subscribe());
						teacher.setJoin_date(user.getJoin_date());
						teacher.setName(user.getName());
						teacher.setSchool_name(user.getSchool_name());
						teacher.setUser_no(user.getUser_no());
						teacher.setUserid(user.getUserid());
						teacher.setUsertype(user.getUsertype());
						teacher.setWxuserid(user.getWxuserid());
						
						teacherList.add(teacher);
					}
				}
			}
			
		}
		long endTime1 = System.currentTimeMillis();
		int count = teacherService.saveTeacherBatch(teacherList);
		System.out.println("成功插入了"+count+"条数据！");
		long endTime2 = System.currentTimeMillis();
		System.out.println("查询及封装共耗时"+(endTime1-startTime)+"毫秒！"); //132606s
		System.out.println("插入共耗时"+(endTime2-endTime1)+"毫秒！"); //3038s
		System.out.println("一共耗时"+(endTime2-startTime)+"毫秒！"); //135644s
	}
	

	
	//============================================================================================
	
	
	
	
	//	获取指定学校的所有用户列表（老师，学生，退休老师，校友）
	public CommonResult<User> getUserList(String objectid) {
		long timestamp = System.currentTimeMillis();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("timestamp", timestamp+"");
		map.put("devCode", devCode);
		map.put("devType", devType);
		map.put("objType", objType);
		map.put("officeid", officeid);
		map.put("size", "10000");
		map.put("objectid", objectid);
		String url = "https://open.campus.qq.com/api/open/buGetUserList?" 
					+ "timestamp=" + timestamp 
					+ "&sign=" + SignUtil.getSign(map,keyForData) 
					+ "&devCode=" + devCode
					+ "&devType=" + devType
					+ "&objType=" + objType
					+ "&officeid=" + officeid
					+ "&objectid="+objectid
					+ "&size=10000";
		
		String jsonStr = HttpClientUtil.get(url);
		System.out.println(jsonStr);
		
		//	把json数据封装成CommonResult
		
		CommonResult<User> result = JSON.parseObject(jsonStr, new TypeReference<CommonResult<User>>() {});
		//	System.out.println(result.getData().getDataList());
		return result;
	}

	
	
	
	//	根据教师查行政课列表
	@Test
	public void getAdministrativeClass() {
		String objectid = "s1DGX44Noxmasodone";
		//String userid = "Ea7EElYESc0done";
		long timestamp = System.currentTimeMillis();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("timestamp", timestamp + "");
		map.put("devCode", devCode);
		map.put("devType", devType);
		map.put("objType", objType);
		map.put("officeid", officeid);
		map.put("size", "10000");
		map.put("objectid", objectid);
		//map.put("userid", userid);
		String url = "https://m.campus.qq.com/api/open/buGetCourseA?" 
					+ "timestamp=" + timestamp 
					+ "&sign=" + SignUtil.getSign(map,keyForData)
					+ "&devCode=" + devCode
					+ "&devType=" + devType
					+ "&objType=" + objType
					+ "&officeid=" + officeid
					+ "&objectid="+objectid
					//+ "&userid="+userid
					+ "&size=10000";
		
		String jsonStr = HttpClientUtil.get(url);
		
		//	把json数据封装成CommonResult
			System.out.println(jsonStr);
		//CommonResult<User> result = JSON.parseObject(jsonStr, new TypeReference<CommonResult<User>>() {});
		//	System.out.println(result.getData().getDataList());
		//return result;
	}
	

	
	
}
